package com.skateboard.reelviewtest.view.reelview;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

import androidx.annotation.Nullable;

import com.skateboard.reelviewtest.R;

public class UxinReelView extends View {

    private Paint paint;

    private int dis = 0;

    private int textColor = Color.BLACK;

    private int reelColor = Color.RED;

    private int paperColor = Color.WHITE;

    private float textSize = 20f;

    private String text = "";


    private float reelWidth = 60f;

    private int duration = 3000;

    private ValueAnimator disAnimator;

    private boolean isExpand = false;

    private float reelTopBarHeight = 20f;

    private float lineOffset = 10f;

    private int bgImage = 0;

    //画卷比背景图高出的距离
    private int heightTap=10;

    public ReelExpandCallBack reelExpandCallBack=null;

    private Bitmap leftBitmap=BitmapFactory.decodeResource(getResources(), R.mipmap.icon_left);

    private Bitmap rightBitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.icon_right);

    private Bitmap bgBitmap   = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_bg);

    private Bitmap bgTextBitmap;

    public UxinReelView(Context context) {
        this(context,null);
    }

    public UxinReelView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ReelView);
        if(typedArray!=null){
            textColor = typedArray.getColor(R.styleable.ReelView_textColor, Color.BLACK);
            reelColor = typedArray.getColor(R.styleable.ReelView_reelColor, Color.RED);
            paperColor = typedArray.getColor(R.styleable.ReelView_paperColor, Color.WHITE);
            text = typedArray.getString(R.styleable.ReelView_text);
            reelWidth = typedArray.getDimension(R.styleable.ReelView_reelWidth, 80f);
            duration = typedArray.getInteger(R.styleable.ReelView_duration, 3000);
            textSize = typedArray.getDimension(R.styleable.ReelView_textSize, 20f);
            bgImage = typedArray.getInteger(R.styleable.ReelView_bgImage,0);
            typedArray.recycle();
        }
        init();
    }


    private void init(){
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        disAnimator = ValueAnimator.ofInt(0, duration / 1000);
        disAnimator.setDuration(duration);
        disAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        disAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                dis = (int) (valueAnimator.getAnimatedFraction() * (getWidth() / 2 - reelWidth));
                postInvalidate();
            }
        });
        disAnimator.addListener(new Animator.AnimatorListener (){


            @Override
            public void onAnimationStart(Animator animator) {
                if(reelExpandCallBack!=null){
                    reelExpandCallBack.callBackExpandStart(isExpand);
                }
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                if(reelExpandCallBack!=null){
                    reelExpandCallBack.callBackExpandEnd(isExpand);
                }
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if(bgBitmap!=null){
            int width=bgBitmap.getWidth()+(int)reelWidth*2;
            if(width>0)
            getLayoutParams().width= width;
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if(disAnimator!=null){
            disAnimator.cancel();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(canvas!=null){
            int count = canvas.saveLayer(null, null, Canvas.ALL_SAVE_FLAG);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP));
            drawPaperBg(canvas);
            paint.setXfermode(null);
            canvas.restoreToCount(count);
            drawReels(canvas);
            drawPaperTxtBg(canvas);
        }
    }


    /**
     * 绘制背景图
     */
    private void drawPaperBg(Canvas canvas){
        if(bgBitmap==null){
            return;
        }
        int centerX = getWidth() / 2;
        // 指定图片绘制区域(左上角的四分之一)
        Rect src = new Rect(bgBitmap.getWidth() / 2-dis, 0, bgBitmap.getWidth() / 2+dis, bgBitmap.getHeight());
        // 指定图片在屏幕上显示的区域(原图大小)
        Rect dst = new Rect(centerX - dis, (int)(reelTopBarHeight+heightTap), centerX + dis, (int)(getHeight() - reelTopBarHeight-heightTap));
        canvas.drawBitmap(bgBitmap, src, dst, null);
    }
    private void drawPaperTxtBg(Canvas canvas){
        if(bgTextBitmap==null||isExpand){
            return;
        }

        int centerX = getWidth() / 2;
        // 指定图片绘制区域(左上角的四分之一)
        Rect src = new Rect(bgTextBitmap.getWidth() / 2-dis, 0, bgTextBitmap.getWidth() / 2+dis, bgTextBitmap.getHeight());
        // 指定图片在屏幕上显示的区域(原图大小)
        Rect dst = new Rect(centerX - dis, (int)(reelTopBarHeight+heightTap), centerX + dis, (int)(getHeight() - reelTopBarHeight-heightTap));
        canvas.drawBitmap(bgBitmap, src, dst, null);
    }
    private void drawReels(Canvas canvas){
        drawLeftReel(canvas);
        drawRightReel(canvas);
    }

    private void drawLeftReel(Canvas canvas)
    {
        int centerX = getWidth() / 2;
        int left = centerX - dis - (int)reelWidth;
        int right = centerX - dis;
        Rect rect =new Rect(left, (int)reelTopBarHeight, right, getHeight() - (int)reelTopBarHeight);
        canvas.drawBitmap(leftBitmap,null,rect,null);
    }

    private void drawRightReel(Canvas canvas)
    {
        int centerX = getWidth() / 2;
        int left = centerX + dis;
        int right = left + (int)reelWidth;
        //drawReel(canvas, left, right)

        Rect rect = new Rect(left, (int)reelTopBarHeight, right, getHeight() - (int)reelTopBarHeight);
        canvas.drawBitmap(rightBitmap,null,rect,null);
    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction()==MotionEvent.ACTION_DOWN) {
                if (!disAnimator.isRunning()) {
                    if (!isExpand) {
                        int centerX = getWidth() / 2;
                        if (event.getX() >= centerX - reelWidth && event.getX() <= centerX + reelWidth) {
                            isExpand = true;
                            startAnimator();

                        }
                    } else {
                        isExpand = false;
                        disAnimator.reverse();
                    }
                }
                return true;
        }
        return false;
    }

    private void startAnimator()
    {
        if (!disAnimator.isStarted())
        {
            disAnimator.start();
        }
    }

    public interface ReelExpandCallBack{
        void callBackExpandEnd(boolean isExpand);
        void callBackExpandStart(boolean isExpand);
    }

    public void setReelExpandCallBack(ReelExpandCallBack reelExpandCallBack) {
        this.reelExpandCallBack = reelExpandCallBack;
    }

    public void setBgTextBitmap(Bitmap bgTextBitmap) {
        this.bgTextBitmap = bgTextBitmap;
    }
}
